package managedbean.SCM.DeliveryOrder;
import entity.SCM.DeliveryOrder;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import session.stateless.SCM.DeliveryOrderBeanLocal;

@ManagedBean (name = "successDeliveryOrderTable")
@ViewScoped
public class SuccessDeliveryOrderManagerBean implements Serializable {

    public SuccessDeliveryOrderManagerBean() {    }
    
    @EJB
    DeliveryOrderBeanLocal deliveryOrderBeanLocal;
    private DeliveryOrder selectedDeliveryOrder;
    private String statusMessage;

    public DeliveryOrder getSelectedDeliveryOrder() {
        return selectedDeliveryOrder;
    }

    public void setSelectedDeliveryOrder(DeliveryOrder selectedDeliveryOrder) {
        this.selectedDeliveryOrder = selectedDeliveryOrder;
    }

    public String getStatusMessage() {
        return statusMessage;
    }

    public void setStatusMessage(String statusMessage) {
        this.statusMessage = statusMessage;
    }
    
    public List<DeliveryOrder> getSuccessDeliveryOrder(){
        List<DeliveryOrder> temp_deliveryOrders = new ArrayList<DeliveryOrder>();
        for (DeliveryOrder temp: deliveryOrderBeanLocal.getSuccessDeliveryOrder())
        {
            if (temp.getCustomerInvoice()==null)
                temp_deliveryOrders.add(temp);
        }
        return temp_deliveryOrders;
    }
    
    //Pass to Daniel's FRM to process invoice
    public void createInvoice(ActionEvent event) throws IOException{
        if (this.getSelectedDeliveryOrder().getCustomerInvoice()==null)
        {
            System.out.println("test1");
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("selectedDeliveryOrder", selectedDeliveryOrder);
            FacesContext.getCurrentInstance().getExternalContext().redirect("../customerInvoice/addCustomerInvoice.xhtml");
        }
        else
        {
            System.out.println("test2");
            statusMessage="An invoice for this delivery order has been created previously.";
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Invalid:" + statusMessage + "", ""));
        }
    
    }
}
